module protocol {
export class ProtocolParam {

    public id:any;
    public fieldDict:Object;
    public fieldList:Array<ProtocolField>;
    public changeFieldList:Array<ProtocolField>;
    public changeFlagsLength:number;

    public constructor(id:number, fieldArray:Array<Array<any>>) {
        this.id = id;
        this.fieldDict = {};
        this.fieldList = [];
        this.changeFieldList = [];
        for (var array of fieldArray) {
            var field:ProtocolField = new ProtocolField(array);
            this.fieldList.push(field);
            this.fieldDict[field.name] = field;
            if (field.isOptional()) {
                this.changeFieldList.push(field);
            }
        }
        this.changeFlagsLength = Math.ceil(this.changeFieldList.length / 8);
    }
}

export class ProtocolField {
    public name:string;
    public type:ProtocolParamType;
    public fieldType:ProtocolFieldType;

    public constructor(array:Array<any>) {
        this.name = array[0];
        this.type = +array[1];
        this.fieldType = +array[2];
    }

    public isOptional():boolean {
        return this.type != ProtocolParamType.required;
    }

    public isRepeated():boolean {
        return this.type == ProtocolParamType.repeated;
    }

    public isCustom():boolean {
        return this.fieldType >= ProtocolFieldType.custom;
    }
}

export enum ProtocolParamType {
    required = 0,
    optional = 1,
    repeated = 2
}

export enum ProtocolFieldType {
    byte = 0,
    boolean = 1,
    int = 2,
    uint = 3,
    short = 4,
    ushort = 5,
    float = 6,
    double = 7,
    string = 8,
    bytes = 9,
    custom = 10
}
}